package com.zjhome.itoken.service.sso.controller;

import java.util.UUID;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.zjhome.itoken.common.domain.TbSysUser;
import com.zjhome.itoken.common.utils.CookieUtils;
import com.zjhome.itoken.common.utils.MapperUtils;
import com.zjhome.itoken.service.sso.service.LoginService;
import com.zjhome.itoken.service.sso.service.consumer.RedisService;

@Controller
public class LoginController {
	@Autowired
	private LoginService 	loginService;
	
	@Autowired
	private RedisService 	redisService;
	
	/**
	 * 跳转登陆页面
	 * @return
	 */
	@RequestMapping(value = "login", method = RequestMethod.GET)
	public String login(HttpServletRequest request, 
			@RequestParam(required = false) String url,
			Model model) {
		String token = CookieUtils.getCookieValue(request, "token");
		if (StringUtils.isNotBlank(token)) {
			String loginCode = redisService.get(token);
			if (StringUtils.isNotBlank(loginCode)) {
				String json = redisService.get(loginCode);
				try {
					TbSysUser tbSysUser = MapperUtils.json2pojo(json, TbSysUser.class);
					if (tbSysUser != null) {
						if (StringUtils.isNotBlank(url)) {
							// 跳转回请求登陆页面
							return "redirect:" + url;
						}
						else {
							model.addAttribute("loginUser", tbSysUser);
						}
					}
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		
		if (StringUtils.isNotBlank(url)) {
			model.addAttribute("url", url);
		}
		
		return "login";
	}
	
	
	/**
	 * 登陆业务
	 * @param loginCode
	 * @param plantPassword
	 * @return
	 */
	@RequestMapping(value = "login", method = RequestMethod.POST)
	public String login(
			@RequestParam(required = true) String loginCode, 
			@RequestParam(required = true) String password, 
			@RequestParam(required = false) String url,
			HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		
		TbSysUser tbSysUser = loginService.login(loginCode, password);
		if (tbSysUser == null) {
			// 登陆失败
			redirectAttributes.addFlashAttribute("message", "用户名或密码错误");			
		}
		else {
			// 登陆成功
			String token = UUID.randomUUID().toString();
			String ack = redisService.put(token, loginCode, 60 * 60 * 24);
			
			if (StringUtils.isNotBlank(ack) && "ok".equals(ack)) {
				// 保存缓存成功
				CookieUtils.setCookie(request, response, "token", token, 60 * 60 * 24);
				if (StringUtils.isNotBlank(url)) {
					// 踢回源网页
					return "redirect:" + url;					
				}
			}
			else {
				// 熔断处理
				redirectAttributes.addFlashAttribute("message", "服务器异常，请稍后再试");
			}
		}
			
		// 返回登陆页
		return "redirect:/login";
	}
	
	@RequestMapping(value = "/logout", method = RequestMethod.GET)	
	public String logout(HttpServletRequest request, HttpServletResponse response, @RequestParam(required = false) String url, Model model) {
		try {
			CookieUtils.deleteCookie(request, response, "token");
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return login(request, url, model);
	}
}
